#include "stdafx.h"
#include "ipgeodll.h"

//
// Flow macros
//

static 
void
WINAPI
_OutputDbgStr(
    __in  LPSTR szMsg,
    __in  DWORD dwStatus)
{
    CHAR rgsz [256];

    StringCbPrintfA(
        rgsz,
        sizeof(rgsz),
        "ERROR: %s - 0x%x\n",
        szMsg,
        dwStatus);
    printf(rgsz);
}

#define CHECK_DWORD(_X) {                                             \
    if (ERROR_SUCCESS != (status = _X)) {                           \
        _OutputDbgStr(#_X, status);                                 \
        __leave;                                                    \
    }                                                               \
}

#define CHECK_BOOL(_X) {                                              \
    if (FALSE == (_X)) {                                            \
        status = GetLastError();                                    \
        _OutputDbgStr(#_X, status);                                 \
        __leave;                                                    \
    }                                                               \
}

#define CHECK_PTR(_X) {                                               \
    if (NULL == (_X)) {                                             \
        status = GetLastError();                                    \
        _OutputDbgStr(#_X, status);                                 \
        __leave;                                                    \
    }                                                               \
}

//
// Main test entry point
//
int _tmain(__in int argc, __in_ecount(argc) _TCHAR* argv[])
{
	DWORD status = 0;
	PFN_IPGEO_GETLATITUDE pfnGetLat = NULL;
	PFN_IPGEO_GETLONGITUDE pfnGetLong = NULL;
	HMODULE hIpGeo = NULL;
	float fltLat = 0;
	BOOL fFoundLat = FALSE;
	float fltLong = 0;
	BOOL fFoundLong = FALSE;

	UNREFERENCED_PARAMETER(argc);
	UNREFERENCED_PARAMETER(argv);

	__try
	{
		CHECK_PTR(hIpGeo = LoadLibrary(L"ipgeodll.dll"));

		CHECK_PTR(pfnGetLat = (PFN_IPGEO_GETLATITUDE) GetProcAddress(
			hIpGeo, "IPGeoGetLatitude"));

		CHECK_PTR(pfnGetLong = (PFN_IPGEO_GETLONGITUDE) GetProcAddress(
			hIpGeo, "IPGeoGetLongitude"));

		CHECK_DWORD(pfnGetLat(&fltLat, &fFoundLat));

		CHECK_DWORD(pfnGetLong(&fltLong, &fFoundLong));
	}
	__finally
	{
	}

	if (TRUE == fFoundLat)
		printf("Latitude: %f\n", fltLat);
	else
		printf("Error: didn't find latitude.\n");

	if (TRUE == fFoundLong)
		printf("Longitude: %f\n", fltLong);
	else
		printf("Error: didn't find longitude.\n");

	return status;
}

